import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const Default = [
  {
    path: '/personal_center',
    name: '个人中心',
    component: () => import('../views/PersonalCenter'),
    meta:{
      title:'个人中心'
    }
  },
  {
    path: '/my_team',
    name: '我的团队',
    component: () => import('../views/MyTeam'),
    meta:{
      title:'我的团队'
    }
  },
  {
    path: '/secondery_user',
    name: '二级用户',
    component: () => import('../views/SeconderyUser'),
    meta:{
      title:'二级用户'
    }
  },
  {
    path: '/membership_center',
    name: '会员中心',
    component: () => import("../views/MembershipCenter"),
    meta:{
      title:'会员中心',
      keepAlive:true
    }
  },
  {
    path: '/user_agreement',
    name: '用户协议',
    component: () => import("../views/UserAgreement"),
    meta:{
      title:'用户协议'
    }
  },
  {
    path: '/activation_code',
    name: '激活码',
    component: () => import("../views/ActivationCode"),
    meta:{
      title:'激活码'
    }
  },
  {
    path: '/members',
    name: '学易知',
    component: () => import("../views/Members"),
    meta:{
      title:'学易知'
    }
  },
  {
    path: '/buy_members/',
    name: '购买会员',
    component: () => import("../views/BuyMembers"),
    meta:{
      title:'购买会员'
    }
  },
  {
    path: '/my_new',
    name: '我的海报',
    component: () => import("../views/MyNew"),
    meta:{
      title:'我的海报'
    }
  },
  {
    path: '/account_details',
    name: '我的明细',
    component: () => import("../views/AccountDetails"),
    meta:{
      title:'我的明细'
    }
  },
  {
    path: '/withdrawal',
    name: 'withdrawal',
    component: () => import("../views/Withdrawal"),
    meta:{
      title:'提现'
    }
  }

]

const routes = Default

const router = new Router({
  mode: 'history',
  base: '/',
  routes: routes,

})


//路由导航钩子
router.beforeResolve((to, from, next) => {
  let LoginAuth = to.meta.Auth || false;

  const token = Vue.prototype.$token;
  console.log('路由导航钩子',LoginAuth,token)
  //判断访问路由是否需要登录
  if (LoginAuth) {
    //判断是否登录
    if (token) {
      console.log('hasToken')
      next();
    } else {
      next({
        name: 'NotLoggedIn'
      })
    }
  } else {
    next();
  }
  //处理Title
  document.title = to.meta.title == undefined ? '' : to.meta.title

  const defaultBackground = '#FFF';
  // ScrollTop();
  /*处理Meta 背景设置*/
  /*     if (to.meta.backgroundColor) {
          document.getElementsByTagName('body')[0].style.background = to.meta.backgroundColor
      } else {
          document.getElementsByTagName('body')[0].style.background = defaultBackground;
      } */

})

//回滚顶部
const ScrollTop = () => {
  document.body.scrollTop = document.documentElement.scrollTop = 0;
}

export default router
